Write-Host "欢迎来到Windows版zsh自动安装系统" -ForegroundColor Blue

try {
    Write-Output "正在从清华镜像源安装zsh"
    if (Test-Path "C:\Program Files\Git\bin\zsh.exe") {
        Write-Output "zsh已安装"
    } else {
        Write-Output "正在下载zsh安装包"
        $url = "https://github.com/zsh-users/zsh/releases/download/v5.8/zsh-5.8-win64.zip"
        $output = "C:\Users\Administrator\Downloads\zsh-5.8-win64.zip"
        
        # 下载zsh安装包
        try {
            $start_time = Get-Date
            (New-Object Net.WebClient).DownloadFile($url, $output)
            Write-Output "下载完成，用时 $($((Get-Date).Subtract($start_time).Seconds)) 秒"
        } catch {
            Write-Host "下载失败: $_" -ForegroundColor Red
            exit
        }

        Write-Output "正在解压zsh安装包"
        # 解压安装包
        try {
            Add-Type -AssemblyName System.IO.Compression.FileSystem
            [System.IO.Compression.ZipFile]::ExtractToDirectory($output, "C:\Program Files\Git\bin\zsh")
            Write-Output "解压完成"
        } catch {
            Write-Host "解压失败: $_" -ForegroundColor Red
            exit
        }

        Write-Output "正在配置环境变量"
        try {
            $env:Path += ";C:\Program Files\Git\bin\zsh"
            [Environment]::SetEnvironmentVariable("Path", $env:Path, "User")
            Write-Output "环境变量配置完成"
        } catch {
            Write-Host "环境变量配置失败: $_" -ForegroundColor Red
            exit
        }
    }
} catch {
    Write-Host "安装过程中发生错误: $_" -ForegroundColor Red
}

Write-Host "© GlueStudio 2024" -ForegroundColor Orange
Write-Output "正在启动zsh..."
Start-Process -FilePath "C:\Program Files\Git\bin\zsh\zsh.exe" -ArgumentList "--login"
